package com.xdap.service.impl;

import com.xdap.common.utils.GsonUtil;
import com.xdap.dao.*;
import com.xdap.pojo.jimu.*;
import com.xdap.service.JmReportImportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class JmReportImportServiceImpl implements JmReportImportService {
    @Autowired
    private DictDao dictDao;
    @Autowired
    private DictItemDao dictItemDao;
    @Autowired
    private ReportDao reportDao;
    @Autowired
    private ReportMapDao reportMapDao;
    @Autowired
    private ReportDataSourceDao reportDataSourceDao;
    @Autowired
    private ReportDbDao reportDbDao;
    @Autowired
    private ReportDbParamDao reportDbParamDao;
    @Autowired
    private ReportLinkDao reportLinkDao;
    @Autowired
    private ReportShareDao reportShareDao;
    @Value("${apaas.single.tenantId}")
    private String dbName;
    @Value("${activiti.datasource.url}")
    private String dbUrl;
    @Value("${activiti.datasource.username}")
    private String dbUserName;
    @Value("${activiti.datasource.password}")
    private String dbPassword;
    @Value("${activiti.datasource.driverClassName}")
    private String driverClassName;

    @Override
    public Map<String, String> insertData(List<MultipartFile> files) {
        Map<String, String> resourceMap = new HashMap<>();

        log.info("共上传附件【{}】个", CollectionUtils.isEmpty(files) ? 0 : files.size());

        for (MultipartFile multipartFile : files) {
            try {
                String result = null;
                byte[] bytes = multipartFile.getBytes();
                String data = new String(bytes);

                // 解析data
                String name = multipartFile.getName();
                // 根据文件的名字，进行处理分类
                switch (name) {
                    case "jimu_report_data_source.json":
                        jimuReportDataSourceInsert(data);
                        break;
                    case "jimu_dict_item.json":
                        jimuDictItemInsert(data);
                        break;
                    case "jimu_dict.json":
                        jimuDictInsert(data);
                        break;
                    case "jimu_report_db_param.json":
                        jimuReportDbParamInsert(data);
                        break;
                    case "jimu_report_db.json":
                        jimuReportDbInsert(data);
                        break;
                    case "jimu_report_link.json":
                        jimuReportLinkInsert(data);
                        break;
                    case "jimu_report_map.json":
                        jimuReportMapInsert(data);
                        break;
                    case "jimu_report.json":
                        jimuReportInsert(data);
                        break;
                    case "jimu_report_share.json":
                        jimuReportShareInsert(data);
                        break;
                    default:
                        break;
                }
                resourceMap.put(multipartFile.getOriginalFilename(), result);
            } catch (Exception e) {
                log.error("缓存转流报错", e);
            }
        }

        return resourceMap;
    }

    private void jimuDictItemInsert(String data) {
        List<JimuDictItemPo> poList = GsonUtil.toList(data, JimuDictItemPo.class);
        for (JimuDictItemPo po : poList) {
            try {
                //  如果查询不到数据，是新增，如果查询得到数据，是更新
                dictItemDao.saveOrUpdate(po);
            } catch (Exception e) {
                log.error("数据内容：{}", GsonUtil.toJson(po));
                log.error("保存【jimu_dict_item】数据报错：{}", e);
            }
        }
    }
    private void jimuReportShareInsert(String data) {
        List<JimuReportSharePo> poList = GsonUtil.toList(data, JimuReportSharePo.class);
        for (JimuReportSharePo po : poList) {
            try {
                //  如果查询不到数据，是新增，如果查询得到数据，是更新
                reportShareDao.saveOrUpdate(po);
            } catch (Exception e) {
                log.error("数据内容：{}", GsonUtil.toJson(po));
                log.error("保存【jimu_report_share】数据报错：{}", e);
            }
        }
    }
    private void jimuReportInsert(String data) {
        List<JimuReportPo> poList = GsonUtil.toList(data, JimuReportPo.class);
        for (JimuReportPo po : poList) {
            try {
                //  如果查询不到数据，是新增，如果查询得到数据，是更新
                reportDao.saveOrUpdate(po);
            } catch (Exception e) {
                log.error("数据内容：{}", GsonUtil.toJson(po));
                log.error("保存【jimu_report】数据报错：{}", e);
            }
        }
    }
    private void jimuReportMapInsert(String data) {
        List<JimuReportMapPo> poList = GsonUtil.toList(data, JimuReportMapPo.class);
        for (JimuReportMapPo po : poList) {
            try {
                //  如果查询不到数据，是新增，如果查询得到数据，是更新
                reportMapDao.saveOrUpdate(po);
            } catch (Exception e) {
                log.error("数据内容：{}", GsonUtil.toJson(po));
                log.error("保存【jimu_report_map】数据报错：{}", e);
            }
        }
    }
    private void jimuReportLinkInsert(String data) {
        List<JimuReportLinkPo> poList = GsonUtil.toList(data, JimuReportLinkPo.class);
        for (JimuReportLinkPo po : poList) {
            try {
                //  如果查询不到数据，是新增，如果查询得到数据，是更新
                reportLinkDao.saveOrUpdate(po);
            } catch (Exception e) {
                log.error("数据内容：{}", GsonUtil.toJson(po));
                log.error("保存【jimu_report_link】数据报错：{}", e);
            }
        }
    }
    private void jimuReportDbInsert(String data) {
        List<JimuReportDbPo> poList = GsonUtil.toList(data, JimuReportDbPo.class);
        for (JimuReportDbPo po : poList) {
            try {
                //  如果查询不到数据，是新增，如果查询得到数据，是更新
                reportDbDao.saveOrUpdate(po);
            } catch (Exception e) {
                log.error("数据内容：{}", GsonUtil.toJson(po));
                log.error("保存【jimu_report_db】数据报错：{}", e);
            }
        }
    }
    private void jimuReportDbParamInsert(String data) {
        List<JimuReportDbParamPo> poList = GsonUtil.toList(data, JimuReportDbParamPo.class);
        for (JimuReportDbParamPo po : poList) {
            try {
                //  如果查询不到数据，是新增，如果查询得到数据，是更新
                reportDbParamDao.saveOrUpdate(po);
            } catch (Exception e) {
                log.error("数据内容：{}", GsonUtil.toJson(po));
                log.error("保存【jimu_dict_item】数据报错：{}", e);
            }
        }
    }
    private void jimuDictInsert(String data) {
        List<JimuDictPo> poList = GsonUtil.toList(data, JimuDictPo.class);
        for (JimuDictPo po : poList) {
            try {
                //  如果查询不到数据，是新增，如果查询得到数据，是更新
                dictDao.saveOrUpdate(po);
            } catch (Exception e) {
                log.error("数据内容：{}", GsonUtil.toJson(po));
                log.error("保存【jimu_dict_item】数据报错：{}", e);
            }
        }
    }

    private String jimuReportDataSourceInsert(String data) {

        List<JimuReportDataSourcePo> jimuReportDataSourceList = GsonUtil.toList(data, JimuReportDataSourcePo.class);
        // 获取非默认数据库的Id作为关联字段，用于系统中后续的数据插入
        String resourceId = null;
        for (JimuReportDataSourcePo sourcePo : jimuReportDataSourceList) {
            try {

                if (!(sourcePo.getDbUrl().contains("127.0.0.1"))) {
                    // 获取对应的信息，更新配置内的参数
                    sourcePo.setName(dbName);
                    sourcePo.setDbUrl(dbUrl);
                    sourcePo.setDbUsername(dbUserName);
                    sourcePo.setDbPassword(dbPassword);
                    sourcePo.setDbDriver(driverClassName);
                    resourceId = sourcePo.getId();
                }
                //  如果查询不到数据，是新增，如果查询得到数据，是更新
                reportDataSourceDao.saveOrUpdate(sourcePo);

            } catch (Exception e) {
                log.error("数据内容：{}", GsonUtil.toJson(sourcePo));
                log.error("保存【jimu_report_data_source】数据报错：{}", e);
            }
        }

        return resourceId;
    }

    // 

}
